package com.jffccloud.auth.security;

import com.jffccloud.common.config.ModifyHttpServletRequestWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Base64;

@Component
public class OauthFilter extends OncePerRequestFilter {
    @Autowired
    private SecurityProperties securityProperties;
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        ModifyHttpServletRequestWrapper httpServletRequestWrapper = new ModifyHttpServletRequestWrapper(request);
        byte[] securitys=(securityProperties.getOauth2().getClients()[0].getClientId()+":"+securityProperties.getOauth2().getClients()[0].getClientSecret()).getBytes("UTF-8");
        httpServletRequestWrapper.putHeader(HttpHeaders.AUTHORIZATION,"Basic "+Base64.getEncoder().encodeToString(securitys));
        filterChain.doFilter(httpServletRequestWrapper, response);
    }
}
